package com.example.administrator.myapplication;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.example.administrator.myapplication.fragmentlei.Fragment1;
import com.example.administrator.myapplication.fragmentlei.Fragment2;

/**
 * Created by 董豪华 on 2017/1/3.
 */
public class ZhuJianMian extends AppCompatActivity {
    com.ashokvarma.bottomnavigation.BottomNavigationBar bb_db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhujianmian);
        dibu();

    }

    public String  init() {
        Intent in = getIntent();
        String city = in.getStringExtra("city");
        return city;

    }

    private void dibu() {
        bb_db = (BottomNavigationBar) findViewById(R.id.bb_db);
        bb_db.setMode(BottomNavigationBar.MODE_FIXED);
        bb_db
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC
                );
        bb_db.addItem(new BottomNavigationItem(R.drawable.home, "首页"))
                .addItem(new BottomNavigationItem(R.drawable.star, "玩吧"))
                .setFirstSelectedPosition(0)
                .initialise();
        setDefaultFragment();
        bb_db.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                switch (position){
                    case 0:
                    {
                        android.app.FragmentManager manager = getFragmentManager();
                        android.app.FragmentTransaction fragmentTransaction = manager.beginTransaction();
                        fragmentTransaction.replace(R.id.ll_layout,new Fragment1());
                        fragmentTransaction.commit();
                    }
                    break;
                    case 1:
                    {
                        android.app.FragmentManager manager = getFragmentManager();
                        android.app.FragmentTransaction fragmentTransaction = manager.beginTransaction();
                        fragmentTransaction.replace(R.id.ll_layout,new Fragment2(ZhuJianMian.this));
                        fragmentTransaction.commit();
                    }
                    break;
                }
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }

    /**
     * 设置默认的
     */
    private void setDefaultFragment() {
        android.app.FragmentManager manager = getFragmentManager();
        android.app.FragmentTransaction fragmentTransaction = manager.beginTransaction();
        fragmentTransaction.replace(R.id.ll_layout,new Fragment1());
        fragmentTransaction.commit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("你要退出吗？");
        builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                builder.create();
            }
        });
        builder.setNegativeButton("是的", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                System.exit(0);
            }
        });
        builder.show();
        return true;
    }
}